#include <linux/module.h>
#include <linux/init.h>

#include <linux/kernel.h>
#include <linux/fs.h> 

#include <asm/uaccess.h>
#include <linux/string.h>
#include <linux/syscalls.h>

MODULE_LICENSE("GPL");

/*
static long agui_open(const char __user *filename, int flags, int mode)
{
        int fd;
        fd = get_unused_fd();
        if (fd > 0)
        {
                fd_install(fd,filp_open(filename, flags, mode));
        }
        return fd;
}*/

static int __init file_rw_init(void)
{
        struct file *f;
        char buf[100];
        int len;
        mm_segment_t old_fs;

        old_fs = get_fs();
        set_fs(KERNEL_DS);

        f= filp_open("/root/test.log", O_RDWR|O_CREAT|O_TRUNC, 00666);
        if(IS_ERR(f)){
                printk("file open error\n");
                return 0;
        }

//        f->f_op->read(f, buf, len, &f->f_pos);
//        f->f_op->write(f, otherbuf, otherlen, &f->f_pos);
        
        len = sprintf(buf, "%s", "Hello world\n");
        vfs_write(f, buf, len, &f->f_pos);
        filp_close(f, NULL);

        set_fs(old_fs);

	return 0;
}

static void __exit file_rw_cleanup(void)
{
        return ;
}

module_init(file_rw_init);
module_exit(file_rw_cleanup);
